package com.bridgeintelligent.tag.customer.service;


import com.bridgeintelligent.tag.customer.mapper.CustomersMapper;
import com.bridgeintelligent.tag.customer.pojo.Filter;
import lombok.AllArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时更新客群的有效状态
 */
@AllArgsConstructor

@Component
public class CustomersScheduled {

    private CustomersMapper customersMapper;
    private CustomersService customersService;

    @Scheduled(cron = "0 0 0 * * ? ")
    public void requestCustomersStatus() {
        System.out.println("---------开启定时-----------");
        //获取所有客群id
        List<String> customersIds = customersMapper.findAllCustomerId();
        List<Filter> filters;
        String newTime;
        if (customersIds != null && !customersIds.isEmpty()) {
            for (String customersId : customersIds) {
                filters = new ArrayList<>();
                customersService.findFilterGroup("-", customersId, filters);
                if (!filters.isEmpty()) {
                    for (Filter filter : filters) {
                        //根据标签id获取其有效状态
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        newTime = sdf.format(new Date());
                        //判断标签是否过期
                        int count = customersMapper.findDimStatus(newTime, filter.getDimId());
                        if (count > 0) {
                            //已过期,设置客群为无效
                            customersMapper.updateCustomersStatus(customersId, "02");
                        }
                    }
                }
            }
        }
    }

}
